package com.joker.airoleplayplatformback.domain.entity;


import com.joker.airoleplayplatformback.domain.enums.AppHttpCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@Data
@NoArgsConstructor
public class R<T> implements Serializable {


    private static final long serialVersionUID = 6682215287252208284L;


    @Schema(example = "200", description = "操作数字")
    private int code;

    @Schema(example = "查询成功", description = "操作信息")
    private String message;

    @Schema(description = "返回数据")
    private T data;

    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }


    public static R<String> ok() {
        return new R<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    public static R<String> error() {
        return new R<>(AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getCode(), AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
    }

    public static R<String> error(AppHttpCodeEnum appHttpCodeEnum) {
        return new R<>(appHttpCodeEnum.code, appHttpCodeEnum.msg);
    }

    public static <T> R<T> error(T data) {
        R<T> r = new R<>(AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getCode(), AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
        r.setData(data);
        return r;
    }

    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, msg);
    }
    
    public static R<String> fail(String message) {
        return new R<>(AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getCode(), message);
    }
    
    public static <T> R<T> fail(String message, T data) {
        R<T> r = new R<>(AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getCode(), message);
        r.setData(data);
        return r;
    }
}